from App.ext import db
from flask import Blueprint, request, jsonify, make_response, session, render_template
import os
import json
import re
from App.FlyInfoDAO import FlyInfoDAO
from tkinter import _flatten
from itertools import groupby
from App.PCA import PACDAO


webBlue = Blueprint("web", __name__)

FlyInfoDAO = FlyInfoDAO()

def init_webBlue(app):
    app.register_blueprint(blueprint=webBlue)


@webBlue.route('/<page>.html')
def getPage(page):
    try:
        return render_template(page + '.html')
    except:
        return '<h1>页面走丢了</h1>'
    


@webBlue.route("/api/queryAirportNum", methods=["POST", "GET"])
def queryFlys():
    res_list = []
    flys = FlyInfoDAO.get_ArrivalAirport_nums()
    for fly in flys:
        res_item = {
            "city": fly[0],
            "num": fly[1]
        }
        res_list.append(res_item)
    res = {}
    res['data'] = res_list
    return make_response(jsonify(res))



@webBlue.route("/api/queryTotalPrices", methods=["POST", "GET"])
def queryTotalPrices():
    res_list = []
    flys = FlyInfoDAO.queryTotalPrices()
    for fly in flys:
        res_item = {
            "fullName": fly[0],
            "totalFee": str(fly[1])
        }
        res_list.append(res_item)
    res = {}
    res['data'] = res_list
    return make_response(jsonify(res))



@webBlue.route("/api/queryPrice/<price>", methods=["POST", "GET"])
def queryPrice(price):
    price = int(price)
    res_list = []
    flys = FlyInfoDAO.queryPrice()
    res = {}
    flys_list = list(_flatten(flys))
    for k, g in groupby(sorted(flys_list), key=lambda x: x//price):
        item = {
            "label": str(k*price) + '-' + str(( k + 1) * price - 1),
            'count': len(list(g))
        }
        res_list.append(item)
    res['data'] = res_list
    return make_response(jsonify(res))

@webBlue.route("/api/queryCityAirportSort", methods=["POST", "GET"])
def queryCityAirportSort():
    res_list = []
    flys = FlyInfoDAO.queryCityAirportSort()
    c  = []
    for fly in flys:
        res_item = {
            "city": fly[2],
            "num": fly[0],
            "arrAirport": fly[1]
        }
        c.append(fly[2])
        res_list.append(res_item)
    res = {}
    print(c)
    res['data'] = res_list
    return make_response(jsonify(res))

@webBlue.route("/api/queryFullNameSort", methods=["POST", "GET"])
def queryFullNameSort():
    res_list = []
    flys = FlyInfoDAO.queryFullNameSort()
    for fly in flys:
        res_item = {
            "fullname": fly[0],
            "num": fly[1]
        }
        res_list.append(res_item)
    res = {}
    res['data'] = res_list
    return make_response(jsonify(res))


@webBlue.route("/api/getPCs", methods=["POST", "GET"])
def getPCs():
    ls = PACDAO().getPC()
    res_list = []
    n = 1
    for l in ls:
        res_list.append({
            'label': 'PC' + str(n),
            'num': float(l)
        })
        n = n + 1
    res = {}
    res['data'] = res_list
    return make_response(jsonify(res))


@webBlue.route("/api/getScatter", methods=["POST", "GET"])
def getScatter():
    ls = PACDAO().getScatter()
    flys = PACDAO().getFlys()
    res_list = []
    n = 0
    for l in ls:
        flyPirce = flys[n]
        res_list.append(list(l) + [float(flyPirce[5])])
        n = n + 1
    res = {}
    res['data'] = res_list
    return make_response(jsonify(res))

@webBlue.route("/api/getComponents", methods=["POST", "GET"])
def getComponents():
    ls = PACDAO().getComponents()
    res_list = []
    res_list.append(['PC1'] + list(ls[0]))
    res_list.append(['PC2'] + list(ls[1]))
    res = {}
    res['data'] = {}
    res['data']['header'] = ['因子', '起飞时间', '降落时间', '时长', '是否餐饮', '价格']
    res['data']['body'] = res_list
    return make_response(jsonify(res))
